<template>
  <div>
    <div class="table-tool">
      <el-button class="el-button--blue" type="primary" icon="el-icon-upload" @click="exportArea">导出</el-button>
      <el-button class="el-button--blue" type="primary" icon="el-icon-plus" @click="addDialog.visible=true">添加</el-button>
      <el-button class="el-button--blue" type="primary" icon="el-icon-delete" @click="deleteArea">删除</el-button>
    </div>
    <el-table ref="table_container" header-align="center" :data="data" stripe :height="tableHeight" v-loading="loading" @selection-change="handleSelectionChange" header-cell-class-name="table-header-cell" cell-class-name="table-cell">
      <el-table-column type="selection" fixed/>
      <el-table-column prop="provinceCityName" label="省市名称" width="150px"/>
      <el-table-column prop="name" label="区名称" width="150px"/>
      <el-table-column prop="schoolNum" label="区域内学校数量" width="150px"/>
      <el-table-column prop="isExperiment" label="是否试验区" width="150px"/>
      <el-table-column prop="experimentNum" label="授牌实验校" width="150px"/>
      <el-table-column prop="standoutNum" label="授牌示范学校" width="150px"/>
      <el-table-column prop="contact" label="联系人姓名" width="150px"/>
      <el-table-column prop="contactPhone" label="联系人电话" width="150px"/>
      <el-table-column prop="contactEmail" label="邮箱" width="200px"/>
      <el-table-column prop="joinYear" label="加入年份" width="150px"/>
      <el-table-column prop="experimentAt" label="成为试验区时间" width="150px"/>
      <el-table-column prop="standoutAt" label="成为示范区时间" width="150px"/>
      <el-table-column prop="contactAddress" label="联系地址" width="400px"/>
      <el-table-column align="center" label="操作" width="230px" fixed="right">
        <template slot-scope="scope">
          <el-button v-if="roles.indexOf('admin')!==-1" class="text-button" type="text" icon="el-icon-refresh blue" @click="changePasswordDialogShow(scope.$index)">重置密码</el-button>
          <el-button class="text-button" type="text" icon="el-icon-edit blue" @click="changeInfoDialogShow(scope.$index)">修改信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog :title="changeDialog.title" :visible.sync="changeDialog.visible" width="600px">
      <districtChange v-model="changeDialog"/>
    </el-dialog>
    <el-dialog :title="addDialog.title" :visible.sync="addDialog.visible" width="600px">
      <district-add v-model="addDialog"/>
    </el-dialog>
    <el-dialog title="修改密码" :visible.sync="changePasswordDialog.visible" width="30%">
      <el-form ref="passwordForm" :rules="passwordFormRules" :model="changePasswordDialog" label-width="80px">
        <el-form-item label="新密码" prop="password">
          <el-input type="password" class="width80" v-model="changePasswordDialog.password"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="changePasswordDialog.visible=false">取 消</el-button>
        <el-button type="primary" @click="submitChangePassword">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { GetAreaTable, GetAreaExport } from '../../../api/table'
import { SubmitDistrictDelete, CheckHasUserName, SubmitPasswordChange } from '../../../api/form'
import districtChange from '../dialog/district-change'
import districtAdd from '../dialog/district-add'
import { ParseDate, ParseChinese } from '../../../utils/date'
export default {
  name: 'district',
  components: {
    districtChange,
    districtAdd
  },
  data () {
    return {
      tableHeight: '0px',
      pagination: {
        pageSize: 10,
        currentPage: 1
      },
      total: 0,
      data: [],
      loading: true,
      changeDialog: {
        title: '',
        visible: false,
        data: {
          id: '',
          name: '',
          experimentNum: '',
          standoutNum: '',
          schoolNum: '',
          isExperiment: '',
          joinYear: '',
          contact: '',
          contactPhone: '',
          contactEmail: '',
          experimentAt: '',
          standoutAt: '',
          contactAddress: ''
        }
      },
      addDialog: {
        title: '',
        visible: false
      },
      multipleSelection: [],
      changePasswordDialog: {
        visible: false,
        password: '',
        id: ''
      },
      passwordFormRules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.pagination.pageSize = val
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
    },
    flashTable () {
      this.loading = true
      GetAreaTable(this.pagination.pageSize, this.pagination.currentPage).then((res) => {
        this.total = res.data.total
        if (res.data.data != null) {
          res.data.data.forEach(item => {
            if (item.joinYear === 0) {
              item.joinYear = ''
            }
            item.experimentAt = ParseDate(item.experimentAt)
            item.standoutAt = ParseDate(item.standoutAt)
          })
        }
        this.data = res.data.data
        this.loading = false
      })
    },
    changeInfoDialogShow (index) {
      this.changeDialog.title = this.data[index].provinceCityName + this.data[index].name
      this.changeDialog.data.name = this.data[index].name
      this.changeDialog.data.experimentNum = this.data[index].experimentNum
      this.changeDialog.data.standoutNum = this.data[index].standoutNum
      this.changeDialog.data.schoolNum = this.data[index].schoolNum
      this.data[index].isExperiment === '是' ? this.changeDialog.data.isExperiment = true : this.changeDialog.data.isExperiment = false
      this.data[index].joinYear === 0 ? this.changeDialog.data.joinYear = '' : this.data[index].joinYear === '' ? this.changeDialog.data.joinYear = '' : this.changeDialog.data.joinYear = new Date().setFullYear(this.data[index].joinYear)
      this.changeDialog.data.id = this.data[index].id
      this.changeDialog.data.contact = this.data[index].contact
      this.changeDialog.data.contactPhone = this.data[index].contactPhone
      this.changeDialog.data.contactEmail = this.data[index].contactEmail
      this.changeDialog.data.contactAddress = this.data[index].contactAddress
      this.changeDialog.data.experimentAt = ParseChinese(this.data[index].experimentAt)
      this.changeDialog.data.standoutAt = ParseChinese(this.data[index].standoutAt)
      this.changeDialog.visible = true
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    deleteArea () {
      if (this.multipleSelection.length !== 0) {
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].experimentNum !== 0 || this.multipleSelection[i].standoutNum !== 0) {
            this.$message.error('不能删除存在学校的区')
            return
          }
        }
        SubmitDistrictDelete(this.multipleSelection).then(res => {
          this.multipleSelection = []
          this.$store.dispatch('SetFlash')
        })
      }
    },
    reInitTableHeight () {
      if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop < 0) {
        setTimeout(this.reInitTableHeight, 10)
      } else if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop <= 500) {
        this.tableHeight = '500px'
      } else {
        this.tableHeight = document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop + 'px'
      }
    },
    changePasswordDialogShow (index) {
      CheckHasUserName(this.data[index].id, 'area').then((res) => {
        if (res.data.result) {
          this.changePasswordDialog.visible = true
          this.changePasswordDialog.id = res.data.id
          this.changePasswordDialog.password = ''
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    submitChangePassword () {
      SubmitPasswordChange(this.changePasswordDialog.id, this.changePasswordDialog.password).then((res) => {
        this.changePasswordDialog.visible = false
      })
    },
    exportArea () {
      GetAreaExport()
    }
  },
  mounted () {
    setTimeout(this.reInitTableHeight, 100)
  },
  computed: {
    ...mapGetters([
      'height',
      'width',
      'provinceId',
      'cityId',
      'areaName',
      'joinYear',
      'flash',
      'roles'
    ])
  },
  watch: {
    height: function (val) {
      this.$nextTick(this.reInitTableHeight)
    },
    width: function (val) {
      this.$nextTick(this.reInitTableHeight)
    },
    'pagination.currentPage': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    'pagination.pageSize': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    provinceId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    cityId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    areaName: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    joinYear: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    flash: function () {
      this.flashTable()
    }
  }
}
</script>

<style scoped>
  @import "../../../styles/common.css";
  .table-tool {
    width: 100%;
    padding: 15px 10px;
  }
  .width80 {
    width: 80%;
  }
</style>
